package com.qf.shop.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qf.shop.entity.Information;
import com.qf.shop.entity.Orders;
import com.qf.shop.mapper.InformationMapper;
import com.qf.shop.model.TableData;
import com.qf.shop.pojo.EditInformation;
import com.qf.shop.pojo.GetInformation;
import com.qf.shop.pojo.QueryInformation;
import com.qf.shop.service.IInformationService;
import com.qf.shop.vo.GoodsId;
import com.qf.shop.vo.MaxTime;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class InformationImpl implements IInformationService {

    private InformationMapper informationMapper;

    public InformationImpl(InformationMapper informationMapper) {
        this.informationMapper = informationMapper;
    }

    @Override
    public TableData<Information> getAllOrders(QueryInformation queryInformation, Integer offset, Integer limit) {
        String name = queryInformation.getName();
        if(null != name && !"".equals(name.trim())) {
            queryInformation.setName("%" + name + "%");
        }

        String title = queryInformation.getTitle();
        if(null != title && !"".equals(title.trim())) {
            queryInformation.setTitle("%" + title + "%");
        }


        Page<Information> page = PageHelper.offsetPage(offset, limit);

        informationMapper.getAllInformation(queryInformation);

        TableData<Information> tableData = new TableData<>();
        tableData.setRows(page.getResult());
        tableData.setTotal(page.getTotal());
        System.out.println(tableData.toString());
        return tableData;
    }

    @Override
    public void insertInformation(GetInformation getInformation, String userId) {
        Information information = new Information();
        information.setStatus("1");

        information.setName(userId);

        information.setCreateTime(new Date());
        information.setMessage(getInformation.getMessage());
        information.setTitle(getInformation.getTitle());

        informationMapper.insertInformation(information);
    }

    @Override
    public Information getInformation(Integer id) {
        Information information = informationMapper.getInformation(id);
        return information;
    }

    @Override
    public void changeInformation(EditInformation editInformation) {
        Information information = new Information();
        information.setId(editInformation.getId());
        information.setMessage(editInformation.getMessage());
        information.setTitle(editInformation.getTitle());

        informationMapper.changeInformation(information);
    }

    @Override
    public void aChangeInformation(Information information) {
        informationMapper.aChangeInformation(information);
    }

    @Override
    public void insertInformation1(GetInformation getInformation,String userId) {
        Information information = new Information();
        information.setStatus("0");

        information.setName(userId);
        information.setCreateTime(getInformation.getDateTime());
        information.setMessage(getInformation.getMessage());
        information.setTitle(getInformation.getTitle());

        informationMapper.insertInformation1(information);
    }

    @Override
    public GoodsId getInformationId() {
        GoodsId informationId = informationMapper.getInformationId();
        return informationId;
    }

    @Override
    public void againChangeInformation(List<Information> list) {
        informationMapper.againChangeInformation(list);
    }


    @Override
    public MaxTime getMaxTime() {
        MaxTime maxTime = informationMapper.getMaxTime();
        return maxTime;
    }

    @Override
    public List<Information> getUpdateInformation() {
        List<Information> updateInformation = informationMapper.getUpdateInformation();
        return updateInformation;
    }
}
